package org.cid54.android.jshopper;

import android.app.ListActivity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import java.util.List;
import org.cid54.android.jshopper.dao.DaoCallBackMethod;
import org.cid54.android.jshopper.dao.ShoppingListDao;
import org.cid54.android.jshopper.dao.impl.ShoppingListDaoImpl;
import org.cid54.android.jshopper.model.ShoppingList;

public class jShopper extends ListActivity {

    private ShoppingListDao shoppingListDao = new ShoppingListDaoImpl();

    /** Called when the activity is first created.
     * @param savedInstanceState
     */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        this.shoppingListDao.getShoppingLists(new DaoCallBackMethod<List<ShoppingList>>() {

            public void execute(List<ShoppingList> t) {
                jShopper.this.setListView(t);
            }
        });

    }

    @Override
    protected void onPostResume() {
        super.onPostResume();
        this.shoppingListDao.getShoppingLists(new DaoCallBackMethod<List<ShoppingList>>() {

            public void execute(List<ShoppingList> t) {
                jShopper.this.setListView(t);
            }
        });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.jshopper_menu, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {

        switch (item.getItemId()) {

            case R.id.new_list:

                Intent newShoppingList = new Intent(this.getApplicationContext(), NewShoppingList.class);

                startActivity(newShoppingList);

                return true;

            case R.id.new_item:

                Intent newItem = new Intent(this.getApplicationContext(), NewItem.class);

                startActivity(newItem);

                return true;
                
            default:
                return super.onOptionsItemSelected(item);

        }

    }

    private void setListView(List<ShoppingList> shoppingLists) {
        ListView lv = getListView();

        lv.setAdapter(new ShoppingListArrayAdapter(this, R.layout.list_item, shoppingLists));

        lv.setOnItemClickListener(new OnItemClickListener() {

            public void onItemClick(AdapterView<?> parent, View view,
                    int position, long id) {

                ShoppingList shoppingList = (ShoppingList) parent.getAdapter().getItem(position);

                Intent shoppingListDetail = new Intent(view.getContext(), ShoppingListDetail.class);

                shoppingListDetail.putExtra(ShoppingListDetail.SHOPPINGLIST_PARAM_NAME, shoppingList);

                startActivity(shoppingListDetail);
            }
        });

    }

    private class ShoppingListArrayAdapter extends ArrayAdapter<ShoppingList> {

        public ShoppingListArrayAdapter(Context context, int textViewResourceId, List<ShoppingList> objects) {
            super(context, textViewResourceId, objects);
        }
    }
}
